package com.ruoyi.web.controller.qiushi.mall;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.qiushi.domain.equipment.QiushiDeviceType;
import com.ruoyi.qiushi.domain.mall.ProductCategory;
import com.ruoyi.qiushi.domain.mall.ProductSpu;
import com.ruoyi.qiushi.service.equipment.IQiushiDeviceTypeService;
import com.ruoyi.qiushi.service.mall.IProductCategoryService;
import com.ruoyi.qiushi.service.mall.IProductSpuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品Controller
 *
 * @author ruoyi
 * @date 2023-12-08
 */
@RestController
@RequestMapping("/qiushi/mall/productspu")
public class ProductSpuController extends BaseController {
    @Autowired
    private IProductSpuService productSpuService;
    @Autowired
    private IProductCategoryService productCategoryService;
    @Autowired
    private IQiushiDeviceTypeService qiushiDeviceTypeService;

    /**
     * useless
     */
    @PreAuthorize("@ss.hasPermi('qiushi:mall:productspu:listCate')")
    @GetMapping("/listCate")
    public TableDataInfo listCate() {
        startPage();
        List<ProductCategory> list = productCategoryService.selectProductCategoryList(new ProductCategory());
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('qiushi:mall:productspu:listType')")
    @GetMapping("/listType")
    public TableDataInfo listType() {
        startPage();
        List<QiushiDeviceType> list = qiushiDeviceTypeService.selectQiushiDeviceTypeList(new QiushiDeviceType());
        return getDataTable(list);
    }

    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('qiushi:mall:productspu:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductSpu productSpu) {
        startPage();
        List<ProductSpu> list = productSpuService.selectProductSpuList(productSpu);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('qiushi:mall:productspu:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductSpu productSpu) {
        List<ProductSpu> list = productSpuService.selectProductSpuList(productSpu);
        ExcelUtil<ProductSpu> util = new ExcelUtil<ProductSpu>(ProductSpu.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('qiushi:mall:productspu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(productSpuService.selectProductSpuById(id));
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('qiushi:mall:productspu:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductSpu productSpu) {
        productSpu.setCreateTime(new Date());
        productSpu.setUpdateTime(new Date());
        return toAjax(productSpuService.insertProductSpu(productSpu));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('qiushi:mall:productspu:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductSpu productSpu) {
        productSpu.setUpdateTime(new Date());
        return toAjax(productSpuService.updateProductSpu(productSpu));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('qiushi:mall:productspu:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        ProductSpu productSpu = new ProductSpu();
        productSpu.setUpdateTime(new Date());
        productSpu.setDelFlag("1");
        productSpu.setId(id);
        return toAjax(productSpuService.updateProductSpu(productSpu));
    }
}
